﻿using Excel4DB.Util;
using Excel4DB.Util.Interfaces;
using System;
using System.Windows.Forms;

namespace Excel4DB.WinApp
{
    public partial class MainForm : Form
    {
        private readonly IDataBase _dataBase;
        public MainForm()
        {
            this.StartPosition= FormStartPosition.CenterScreen;
            _dataBase = DataBaseFactory.Instance.Create();
            InitializeComponent();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.groupBoxInfo.Enabled = false;
            this.textBox1.Text = @"  通过本软件可快速方便的在Excel和数据库之间进行数据操作，Excel导入数据库，数据库数据导入Excel，
同时 可对Excel表头以及数据表之间进行字段匹配操作";
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioBtnPwd_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.groupBoxInfo.Enabled) this.groupBoxInfo.Enabled = true;
        }

        private void radioBtnWin_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxInfo.Enabled = false;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace(txtBoxServer.Text.Trim()))
            {
                MessageBox.Show("请输入主机地址");
                return;
            }
            if(this.groupBoxInfo.Enabled)
            {
                if (string.IsNullOrWhiteSpace(this.txtAccount.Text.Trim()))
                {
                    MessageBox.Show("请输入账号");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.txtPwd.Text.Trim()))
                {
                    MessageBox.Show("请输入密码");
                    return;
                }
                ConfigHelper.Instance.Account = this.txtAccount.Text.Trim();
                ConfigHelper.Instance.Pwd = this.txtPwd.Text.Trim();
            }
            ConfigHelper.Instance.ServerAddress = this.txtBoxServer.Text.Trim();
            ConfigHelper.Instance.ServerPort = this.txtBoxPort.Text.Trim();
            bool b= _dataBase.ConnectionIsOpen();
            if (b)
            {
                MessageBox.Show("链接成功");

                DBForm dBForm = new DBForm(_dataBase);
                dBForm.ShowDialog();
            }
            else
            {
                MessageBox.Show("链接失败");
            }
        }
    }
}
